package com.boarsoft.boar.batch.demo.flow.demo;

import com.boarsoft.boar.batch.demo.exception.ExitException;
import com.boarsoft.boar.batch.demo.entity.EmpSalary;
import com.boarsoft.boar.batch.service.BatchReportService;
import com.boarsoft.flow.core.bean.ProcessHandler;
import com.boarsoft.hibernate.biz.SimpleBizImpl;
import com.boarsoft.hibernate.dao.SimpleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 分页处理示例：
 * <p>
 * 给每个员工涨薪5%，并登记明细
 *
 * @author wangtao
 */
public class DemoProcess2HD extends SimpleBizImpl implements ProcessHandler {

    @Autowired
    private SimpleDao simpleDao;

    @Autowired
    private BatchReportService batchReportService;

    @Override
    @Transactional
    public Object process(String entry, Object data, Throwable throwable) throws Throwable {
        Boolean exeStatus = Boolean.TRUE;
        try {
            System.out.println(Thread.currentThread().getName());
            System.out.println(" do DemoProcess2HD...");
            List<EmpSalary> empList = (List<EmpSalary>) data;
            for (EmpSalary emp : empList) {
                BigDecimal salary = emp.getSalary().add(emp.getSalary().multiply(new BigDecimal("0.05")));
                emp.setSalary(salary);
                emp.setMark("涨5%");
                simpleDao.update(emp);
                System.out.println("员工 " + emp.getName() + "基础工资涨幅5%设置完成！");
                System.out.println("员工 " + emp.getName() + "基础工资变动明细登记完成！");
            }

            /**
             * 执行进度上报
             * 单个分页执行完后需上报当前页处理的数据量（dealCount，包含不符合业务条件的数据等）   直到全部上送完
             */
            //batchReportService.report("123456", empList.size(), "DemoForkHD", InetUtil.getIp(), Thread.currentThread().getName());
        } catch (Throwable e) {
            if( e instanceof ExitException){
                throw e;
            } else {
                exeStatus = Boolean.FALSE;
            }
        } finally {
            return exeStatus;
        }
    }
}
